<template>
  <div class="app-container customer-div">
    <div class="checkFreight-page">
      <el-card class="box-card left" style="width:400px;">
        <div slot="header" class="clearfix">
          <span>物流查价</span>
        </div>
        <el-form :model="checkForm" size="mini" label-width="90px"  ref="checkForm" :rules="rules">
            <el-form-item label="发货商品：">
                  塑料粒子
            </el-form-item>
            <el-form-item label="发货仓库：" v-if="checkType == 1">
              <el-select v-model="checkForm.warehouseId" @change="changeWarehouseId">
                  <el-option :value="item.warehouseId" v-for="item in wlist" :label="item.warehouseName" :key="item.warehouseId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发货地：" prop="startAddress">
              <div v-if="checkType == 1">
                <el-input :disabled="true" style="width:193px;vertical-align: middle" v-model="checkForm.startAddress"></el-input>
                <i class="el-icon-location-outline" @click="showMap('start')" style="font-size:20px;cursor: pointer;display: inline-block;margin-left:10px;vertical-align: middle"></i>
              </div>
              <div v-else>
                {{checkForm.startAddress}}
              </div>
            </el-form-item>
            <el-form-item label="收货地：" prop="endAddress">
              <div v-if="checkType == 1">
                <el-input :disabled="true" style="width:193px;vertical-align: middle" v-model="checkForm.endAddress"></el-input>
                <i class="el-icon-location-outline" @click="showMap('end')" style="font-size:20px;cursor: pointer;display: inline-block;margin-left:10px;vertical-align: middle"></i>
              </div>
              <div v-else>
                 {{checkForm.endAddress}}
              </div>
            </el-form-item>
            <el-form-item label="发货数量：" prop="inquiryNum">
              <div v-if="checkType == 1">
                <el-input style="width:100px;" v-model="checkForm.inquiryNum"></el-input><span style="padding-left:10px">吨</span>
              </div>
              <div v-else>
                {{ checkForm.inquiryNum }} 吨
              </div>
            </el-form-item>
            <el-form-item label="预计运费：" v-if="checkType == 2">
                  <span style="font-size:18px;font-weight: bold;color:red">￥{{ ymmAmount }}</span>
                  <span style="color: #606266;font-size:14px;padding-left:40px">单价：</span>
                  <span style="font-size:18px;font-weight: bold;color:red">￥{{ ymmPrice }}</span>
                  <span style="color: #606266;font-size:14px;">/吨</span>
            </el-form-item>
        </el-form>
        <div style="text-align: center">
          <el-button type="primary" size="small" style="width:300px" v-if="checkType == 1" @click="checkFreight" :loading="btnLoading2">查运价</el-button>
          <el-button type="primary" size="small" style="width:300px" v-if="checkType == 2" @click="resetFreight">重新查询</el-button>
        </div>
      </el-card>
      <el-card class="box-card" style="min-height:400px;">
        <div slot="header" class="clearfix">
          <span>最新查询记录</span>
        </div>
        <el-table
          :header-cell-style="{'text-align':'center'}"
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
          <el-table-column  label="线路" min-width="200" prop="orderSn">
            <template slot-scope="scope">
              <div>发货地：{{scope.row.sendAddress}}</div>
              <div>收货地：{{scope.row.receiveAddress}}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="数量"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              <div>{{scope.row.inquiryNum}}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="单价"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              <div>{{scope.row.offerPrice}}</div>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :current-page.sync="listQuery.pageNum"
            :page-size="listQuery.pageSize"
            :page-sizes="[10,15,20]"
            :total="count">
          </el-pagination>

        </div>
      </el-card>
    </div>
    <gaodeMap @handleConfirm="handleConfirm" ref="gaodeMap"></gaodeMap>

  </div>
</template>
<script>
  import {checkFreighInquiry,getUserOption,myDeptList,saleOrderList,getOrderStatList,getOrderUsersList,getKingInfo,customersAjaxEdit} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import gaodeMap from '@/views/components/gaodeMap'
  import { mapGetters } from 'vuex'
  import warehouseInfo from "../pums/components/warehouseInfo";
  import {getIinquiryList} from "../../api/partner";
  import {warehouseList} from '@/api/company'
  const defaultListQuery={
    pageNum: 1,
    pageSize: 10,
    tab:'my'
  };
  const defaultCheckForm={
    "startAreaName" : "",
    "startAddress" : "",
    "startLon" : "",
    "startLat" : "",
    "endAreaName" : "",
    "endAddress" : "",
    "endLon" : "",
    "endLat" : "",
    "inquiryNum" : '',
    warehouseName:''
  };
  export default {
    name: "index",
    data() {
      return {
        listLoading: false,
        tableData: [],
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        count: 0,
        checkForm: JSON.parse(JSON.stringify(defaultCheckForm)),
        checkType:1,
        btnLoading2:false,
        btnLoading3:false,
        rules: {
          startAddress:[{required: true, trigger: 'change' ,message: '此项必填'}],
          endAddress:[{required: true, trigger: 'change' ,message: '此项必填',}],
          inquiryNum:[{required: true, trigger: 'blur',message: '此项必填'}],
        },
        mapType:'',
        ymmAmount:'',
        ymmPrice:'',
        wlist:[]
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId',
        'isMange',
      ]),


    },
    created() {
      this.getList();
      this.getWarehouseList();
    },
    components : {
      gaodeMap:gaodeMap
    },
    filters: {},
    methods: {
      changeWarehouseId(id){
        let i = this.wlist.findIndex((el)=>{
          return el.warehouseId == id;
        })
        if(i != -1){
          this.checkForm.startAreaName = this.wlist[i].address;
          let text = this.checkForm.startAreaName.split("_");
          this.checkForm.startAddress = text.join("")+ this.wlist[i].addressDetail;
          this.checkForm.startLon = this.wlist[i].locationLon;
          this.checkForm.startLat = this.wlist[i].locationLat;
        }
      },
      //获取列表
      getWarehouseList() {
        warehouseList({ pageNum: 1, pageSize: 1000, keywords: '', isHasLocation:'1'}).then(response => {
          this.wlist = response.list;
        }).catch(()=>{
        });
      },
      //查运费
      checkFreight(){
        this.$refs.checkForm.validate((valid) => {
          if (valid) {
            this.btnLoading2 = true;
            let data = JSON.parse(JSON.stringify(this.checkForm));
            checkFreighInquiry(data).then((res)=>{
              this.ymmAmount = res.ymmAmount;
              this.ymmPrice=res.ymmPrice
              this.checkType = 2;
              this.btnLoading2 = false;
            }).catch(()=>{
              this.btnLoading2 = false;
            })
          }
        })

      },
      //重新查询
      resetFreight(){
        this.checkType = 1;
      },
      //显示地图
      showMap(type) {
        this.mapType = type;
        this.$refs.gaodeMap.showMap()
      },

      handleConfirm(adrInfo){
        let city = adrInfo.city ? adrInfo.city : adrInfo.province
         this.checkForm[this.mapType+'AreaName'] = adrInfo.province + '_' + city + '_' + adrInfo.district;
         this.checkForm[this.mapType+'Address'] = adrInfo.adrText;
         this.checkForm[this.mapType+'Lon'] = adrInfo.Lng;
         this.checkForm[this.mapType+'Lat'] = adrInfo.Lat;
         console.log(this.checkForm)
      },
      //获取订单列表
      getList() {
        this.listLoading = true;
        let data = {};
        data = JSON.parse(JSON.stringify(this.listQuery))
        getIinquiryList(data).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },
    },
  }
</script>

<style scoped lang="scss">
    .checkFreight-page{
      padding-left:450px;
      position: relative;
      .left.box-card{
        position: absolute;
        left:0;
        top:0;
      }
    }



</style>
